/*
 * Copyright 2011 Romain Gilles
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jvdb;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

class PreparedStatementWrapper extends StatementWrapper implements PreparedStatement {

    private final PreparedStatement underlying;
    private final IPreparedStatementTimer timer;

    static PreparedStatementWrapper newPreparedStatementWrapper(SimpleConnectionWrapper parent, PreparedStatement delegate, String sql, int statementId, IPreparedStatementTimer timer) {
        return new PreparedStatementWrapper(parent, delegate, sql, statementId, timer);

    }

    PreparedStatementWrapper(SimpleConnectionWrapper parent, PreparedStatement delegate, String sql, int statementId, IPreparedStatementTimer timer) {
        super(parent, delegate, statementId);
        this.underlying = delegate;
        this.timer = timer;
    }

    public boolean execute() throws SQLException {
        timer.start();
        boolean result = underlying.execute();
        timer.end();
        return result;
    }

    public ResultSet executeQuery() throws SQLException {
        timer.start();
        ResultSet result = underlying.executeQuery();
        timer.end();
        return result;
    }

    public int executeUpdate() throws SQLException {
        timer.start();
        int result = underlying.executeUpdate();
        timer.end();
        return result;
    }

    public void setNull(int parameterIndex, int sqlType) throws SQLException {
        underlying.setNull(parameterIndex, sqlType);
        timer.setNull(parameterIndex, sqlType);
    }

    public void setBoolean(int parameterIndex, boolean x) throws SQLException {
        underlying.setBoolean(parameterIndex, x);
        timer.setBoolean(parameterIndex, x);
    }

    public void setByte(int parameterIndex, byte x) throws SQLException {
        underlying.setByte(parameterIndex, x);
        timer.setByte(parameterIndex, x);
    }

    public void setShort(int parameterIndex, short x) throws SQLException {
        underlying.setShort(parameterIndex, x);
        timer.setShort(parameterIndex, x);
    }

    public void setInt(int parameterIndex, int x) throws SQLException {
        underlying.setInt(parameterIndex, x);
        timer.setInt(parameterIndex, x);
    }

    public void setLong(int parameterIndex, long x) throws SQLException {
        underlying.setLong(parameterIndex, x);
        timer.setLong(parameterIndex, x);
    }

    public void setFloat(int parameterIndex, float x) throws SQLException {
        underlying.setFloat(parameterIndex, x);
        timer.setFloat(parameterIndex, x);
    }

    public void setDouble(int parameterIndex, double x) throws SQLException {
        underlying.setDouble(parameterIndex, x);
        timer.setDouble(parameterIndex, x);
    }

    public void setBigDecimal(int parameterIndex, BigDecimal x)
            throws SQLException {
        underlying.setBigDecimal(parameterIndex, x);
        timer.setBigDecimal(parameterIndex, x);
    }

    public void setString(int parameterIndex, String x) throws SQLException {
        underlying.setString(parameterIndex, x);
        timer.setString(parameterIndex, x);
    }

    public void setBytes(int parameterIndex, byte[] x) throws SQLException {
        underlying.setBytes(parameterIndex, x);
        timer.setBytes(parameterIndex, x);
    }

    public void setDate(int parameterIndex, Date x) throws SQLException {
        underlying.setDate(parameterIndex, x);
        timer.setDate(parameterIndex, x);
    }

    public void setTime(int parameterIndex, Time x) throws SQLException {
        underlying.setTime(parameterIndex, x);
        timer.setTime(parameterIndex, x);
    }

    public void setTimestamp(int parameterIndex, Timestamp x)
            throws SQLException {
        underlying.setTimestamp(parameterIndex, x);
        timer.setTimestamp(parameterIndex, x);
    }

    public void setAsciiStream(int parameterIndex, InputStream x, int length)
            throws SQLException {
        underlying.setAsciiStream(parameterIndex, x, length);
        timer.setAsciiStream(parameterIndex, x, length);
    }

    @SuppressWarnings("deprecation")
    public void setUnicodeStream(int parameterIndex, InputStream x, int length)
            throws SQLException {
        underlying.setUnicodeStream(parameterIndex, x, length);
        timer.setUnicodeStream(parameterIndex, x, length);
    }

    public void setBinaryStream(int parameterIndex, InputStream x, int length)
            throws SQLException {
        underlying.setBinaryStream(parameterIndex, x, length);
        timer.setBinaryStream(parameterIndex, x, length);
    }

    public void clearParameters() throws SQLException {
        underlying.clearParameters();
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType,
                          int scale) throws SQLException {
        underlying.setObject(parameterIndex, x, targetSqlType, scale);
        timer.setObject(parameterIndex, x, targetSqlType, scale);
    }

    public void setObject(int parameterIndex, Object x, int targetSqlType)
            throws SQLException {
        underlying.setObject(parameterIndex, x, targetSqlType);
        timer.setObject(parameterIndex, x, targetSqlType);
    }

    public void setObject(int parameterIndex, Object x) throws SQLException {
        underlying.setObject(parameterIndex, x);
        timer.setObject(parameterIndex, x);
    }

    public void addBatch() throws SQLException {
        timer.addBatch();
        underlying.addBatch();
    }

    public int[] executeBatch() throws SQLException {
        timer.start();
        int[] result = underlying.executeBatch();
        timer.end();
        return result;
    }

    public void setCharacterStream(int parameterIndex, Reader reader, int length)
            throws SQLException {
        underlying.setCharacterStream(parameterIndex, reader, length);
        timer.setCharacterStream(parameterIndex, reader, length);
    }

    public void setRef(int i, Ref x) throws SQLException {
        underlying.setRef(i, x);
        timer.setRef(i, x);
    }

    public void setBlob(int i, Blob x) throws SQLException {
        underlying.setBlob(i, x);
        timer.setBlob(i, x);
    }

    public void setClob(int i, Clob x) throws SQLException {
        underlying.setClob(i, x);
        timer.setClob(i, x);
    }

    public void setArray(int i, Array x) throws SQLException {
        underlying.setArray(i, x);
        timer.setArray(i, x);
    }

    public ResultSetMetaData getMetaData() throws SQLException {
        return underlying.getMetaData();
    }

    public void setDate(int parameterIndex, Date x, Calendar cal)
            throws SQLException {
        underlying.setDate(parameterIndex, x, cal);
        timer.setDate(parameterIndex, x, cal);
    }

    public void setTime(int parameterIndex, Time x, Calendar cal)
            throws SQLException {
        underlying.setTime(parameterIndex, x, cal);
        timer.setTime(parameterIndex, x, cal);
    }

    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
            throws SQLException {
        underlying.setTimestamp(parameterIndex, x, cal);
        timer.setTimestamp(parameterIndex, x, cal);
    }

    public void setNull(int paramIndex, int sqlType, String typeName)
            throws SQLException {
        underlying.setNull(paramIndex, sqlType, typeName);
        timer.setNull(paramIndex, sqlType, typeName);
    }

    public void setURL(int parameterIndex, URL x) throws SQLException {
        underlying.setURL(parameterIndex, x);
        timer.setURL(parameterIndex, x);
    }

    public ParameterMetaData getParameterMetaData() throws SQLException {
        return underlying.getParameterMetaData();
    }
}
